import request from '@/utils/request'
import baseapi from '@/api/base'
import { getDataByParameter, saveObjetInfo, deleteObjetInfo } from '@/utils/baserequest'
import storage from 'store'

const api = {
  departmentServer: '/department',
  departmentList: '/by/parentDepartPid/',
  departmentListByExample: '/by/example/',
  departmentCount: '/countBy/parentDepartPid/',
  // orgPid
  orgPid: storage.get('org')
  // orgInfo: '/'
}

export default api

export function getDepartByParentDepartPid (parameter, headers) {
  let appIdHeaders = {
    appId: baseapi.huiwcore_app
  }
  if (headers && headers.authOption) {
  } else {
    appIdHeaders = Object.assign(appIdHeaders, { authOption: '1' })
  }
  const parameters = {
    parentDepartPid: parameter === 'org' ? storage.get('org') : parameter,
    pageSize: 10000,
    sidx: 'sortNum',
    order: 1
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.departmentServer + api.departmentList
  const allheaders = Object.assign(headers, appIdHeaders)
  console.log(url, parameters, allheaders)
  return getDataByParameter(url, parameters, allheaders)
  // .then(res => {
  //   return res
  // })
}

export function getDepartByExample (parameter, headers) {
  let appIdHeaders = {
    appId: baseapi.huiwcore_app
  }
  if (headers && headers.authOption) {
  } else {
    appIdHeaders = Object.assign(appIdHeaders, { authOption: '1' })
  }
  const parameters = {
    example: parameter,
    pageSize: 10000,
    sidx: 'sortNum',
    order: 1
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.departmentServer + api.departmentListByExample
  const allheaders = Object.assign(headers, appIdHeaders)
  console.log(url, parameters, allheaders)
  return getDataByParameter(url, parameters, allheaders)
  // .then(res => {
  //   return res
  // })
}

export function getDepartInfoByPid (parameter, headers) {
  const allheaders = {
    appId: baseapi.huiwcore_app
  }
  return request({
    url: baseapi.baseUrl + baseapi.huiwcore + api.departmentServer + '/' + parameter,
    headers: Object.assign(headers, allheaders),
    method: 'get'
    // params: parameter
  })
}

// id == 0 add     post
// id != 0 update  put
export function saveDepartInfo (parameter, headers) {
  const allheaders = {
    appId: baseapi.huiwcore_app
  }
  const data = {
    orgPid: storage.get('org')
  }
  if (parameter.parentDepartPid === 'org') {
    parameter.parentDepartPid = storage.get('org')
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.departmentServer + '/'
  return saveObjetInfo(url, Object.assign(data, parameter), Object.assign(headers, allheaders))
}

// 通过主键删除部门成员关系 人员信息和单位关系保留
export function delDepartBySysSeqOrPid (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.departmentServer + '/'
  const allheaders = Object.assign(headers, appIdHeaders)
  return deleteObjetInfo(url, parameter, allheaders)
}
